package com.hyl.springbootappinfo.controller;

import com.alibaba.fastjson.JSON;
import com.hyl.springbootappinfo.pojo.*;
import com.hyl.springbootappinfo.service.*;
import com.hyl.springbootappinfo.web.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backend")
public class BackendController {
    @Resource
    private Backend_userService backend_userService;

    @Autowired
    private Dev_userService dev_userService;

    @Autowired
    private Data_dictionaryService data_dictionaryService;

    @Autowired
    private App_categoryService app_categoryService;

    @Autowired
    private App_versionService app_versionService;

    /**
     * 登录验证
     * @param userCode
     * @param userPassword
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    public String dologin(@RequestParam("userCode")String userCode, @RequestParam("userPassword") String userPassword,
                          HttpSession session, HttpServletRequest request){
        Backend_user backend_user=backend_userService.dologin(userCode,userPassword);
        if (backend_user!=null){
            session.setAttribute("userSession",backend_user);
            return "redirect:/backend/from";
        }else{
            request.setAttribute("error","账号密码错误");
            return "backendlogin";
        }

    }

    /**
     * 跳转首页
     * @param request
     * @return
     */
    @RequestMapping("/from")
    public String main(HttpServletRequest request){
            return "/backend/main";

    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("/loginout")
    public String loginout(HttpSession session){
        Backend_user loginUser=(Backend_user)session.getAttribute(Constants.USER_SESSION);
        if (loginUser!=null){
                session.removeAttribute(Constants.USER_SESSION);
        }
        return "backendlogin";
    }

    @RequestMapping("/appinfolist")
    public String getappinfolist(@RequestParam(value = "queryStatus",required = false) Integer status,
                                 @RequestParam(value = "querySoftwareName",required = false) String softwareName,
                                 @RequestParam(value = "queryFlatformId",required = false) Integer flatformId,
                                 @RequestParam(value = "queryCategoryLevel3",required = false) Integer categoryLevel3,
                                 @RequestParam(value = "queryCategoryLevel1",required = false) Integer categoryLevel1,
                                 @RequestParam(value = "queryCategoryLevel2",required = false) Integer categoryLevel2,
                                 @RequestParam(value = "currentPageNo",required = false)Integer currentPageNo,
                                 HttpServletRequest request){
        Backend_user login= (Backend_user) request.getSession().getAttribute(Constants.USER_SESSION);

        if (status==null){
            status=null;
        }
        if (softwareName==null){
            softwareName="";
        }
        if (flatformId==null){
            flatformId=null;
        }
        if (categoryLevel1==null){
            categoryLevel1=null;
        }
        if (categoryLevel2==null){
            categoryLevel2=null;
        }
        if (categoryLevel3==null){
            categoryLevel3=null;
        }

        request.setAttribute("querySoftwareName",softwareName);
        request.setAttribute("queryStatus",status);
        request.setAttribute("queryFlatformId",flatformId);
        request.setAttribute("queryCategoryLevel1",categoryLevel1);
        request.setAttribute("queryCategoryLevel2",categoryLevel2);
        request.setAttribute("queryCategoryLevel3",categoryLevel3);

        //游戏信息
        Integer pagesize=6;
        if (currentPageNo==null){
            currentPageNo=1;
        }

        request.setAttribute("currentPageNo",currentPageNo);
        currentPageNo=(currentPageNo-1)*pagesize;



        List<App_info> app_infoList= backend_userService.applists(currentPageNo,pagesize,softwareName,flatformId,categoryLevel3,categoryLevel1,categoryLevel2);

        request.setAttribute("appInfoList",app_infoList);

        //查询记录数
        int totalCount=dev_userService.count(status,softwareName,flatformId,categoryLevel3,categoryLevel1,categoryLevel2);
        request.setAttribute("totalCount",totalCount);

        int totalPageCount=totalCount/pagesize;
        totalPageCount=totalCount%pagesize==0?totalPageCount:totalPageCount+1;
        request.setAttribute("totalPageCount",totalPageCount);
//        //App状态信息
//        List<Data_dictionary> data_dictionaryList=data_dictionaryService.finbyTypeName("APP状态");
//        request.setAttribute("statusList",data_dictionaryList);

        //App所属平台
        List<Data_dictionary> flatFormList=data_dictionaryService.finbyTypeName1("所属平台");
        request.setAttribute("flatFormList",flatFormList);

//        //一级分类
        List<App_category> categoryLevel1List=app_categoryService.finbyparenId(0);
        request.setAttribute("categoryLevel1List",categoryLevel1List);

        request.setAttribute("devuser",login);

        return "backend/applist";
    }

    //查看二级分类
    @RequestMapping("/categorylevellist")
    @ResponseBody
    public Object categoryLeve2List(@RequestParam Integer pid,HttpServletRequest request){
        List<App_category> categoryLevel2List=app_categoryService.finbyparenId(pid);
        String result= JSON.toJSONString(categoryLevel2List);
        return result;
    }

    //查看三级分类
    @RequestMapping("/categoryLeve3List")
    @ResponseBody
    public Object categoryLeve3List(@RequestParam Integer pid,HttpServletRequest request){
        List<App_category> categoryLevel3List=app_categoryService.finbyparenId(pid);
        String result=JSON.toJSONString(categoryLevel3List);
        return result;
    }

    @RequestMapping("/check")
    public String getcheck(@RequestParam("aid")Integer aid,@RequestParam("vid")Integer vid,HttpServletRequest request){
        App_info app_info=backend_userService.appcheck(aid,vid);
        request.setAttribute("appInfo",app_info);
        App_version app_version=app_versionService.findByAppid(aid);
        request.setAttribute("appVersion",app_version);
        return "backend/appcheck";
    }

    @RequestMapping(value = "/statusmodify",method = RequestMethod.POST)
    public String getstatusmodify(@RequestParam("id")Integer id,@RequestParam("status")Integer status){
        if (backend_userService.statusmodify(id, status)>0){
            return "redirect:/backend/appinfolist";
        }else{
            return "redirect:/backend/appinfolist";
        }
    }

}
